<template>
  <el-dialog
    title="管理素材"
    :visible="visible"
    width="440px"
    :before-close="handleClose"
    :close-on-click-modal="false"
  >
    <div class="grouping-body">
      <div class="top">
        <el-input
          v-model="title"
          placeholder="请输入分组名称"
          size="mini"
          show-word-limit
          maxlength="10"
        ></el-input>
        <el-button type="primary" size="mini" @click="createGrouping">创建</el-button>
      </div>
      <div class="grouping-list">
        <el-scrollbar class="grouping-scroll">
          <div class="item" v-for="item in categoryList" :key="item.id">
            <el-input
              v-if="currentId === item.id"
              v-model="item.title"
              placeholder="请输入分组名称"
              size="mini"
              show-word-limit
              maxlength="10"
              @blur="modifyName(item)"
            ></el-input>
            <div class="title" v-else>{{ item.title }}</div>
            <div class="right">
              <span class="el-icon-edit-outline" @click="handleModify(item.id)"></span>
              <span class="el-icon-delete" @click="handleDelete(item.id)"></span>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="reload">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: Boolean,
    type: [String, Number]
  },
  data() {
    return {
      title: '',
      categoryList: [],
      currentId: 0
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
    },
    // 分组数据
    getCategoryData() {
      let params = {
        type: this.type,
        in: 1
      }
      this.$store.dispatch('material/GET_CATEGORY_LIST', { params: params }).then(res => {
        this.categoryList = res.data
      })
    },
    // 创建分组
    createGrouping() {
      let _this = this
      if (this.title === '') {
        _this.$message.error('请输入分组名称')
        return
      }
      _this.$api.postUserTeacherMaterialCategory(
        {
          type: _this.type,
          title: _this.title
        },
        res => {
          _this.$message.success(res.msg)
          _this.title = ''
          _this.getCategoryData()
        },
        error => {
          _this.$message.error(error.msg)
        }
      )
    },
    // 编辑分组名称
    handleModify(id) {
      this.currentId = id
    },
    // 修改分组名称
    modifyName(data) {
      let _this = this
      _this.$api.putUserTeacherMaterialCategory(
        {
          id: data.id,
          title: data.title
        },
        () => {
          this.currentId = 0
        },
        error => {
          _this.$message.error(error.msg)
        }
      )
    },
    // 删除分组
    handleDelete(id) {
      let _this = this
      _this.$messageBox
        .confirm('确认删除分组吗？')
        .then(() => {
          _this.$api.deleteUserTeacherMaterialCategory(
            { id: id },
            res => {
              _this.$message.success(res.msg)
              _this.getCategoryData()
            },
            error => {
              _this.$message.error(error.msg)
            }
          )
        })
        .catch(() => {})
    },
    reload() {
      this.$emit('reload')
      this.handleClose()
    }
  },
  created() {
    this.getCategoryData()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #e4e7ed;
}
.grouping-body {
  .top {
    display: flex;
    .el-button {
      margin-left: 30px;
    }
  }
  .grouping-list {
    margin: 0 -20px;
    .grouping-scroll {
      ::v-deep .el-scrollbar__wrap {
        max-height: 225px;
      }
    }
  }
  .item {
    padding: 0 20px;
    height: 45px;
    display: flex;
    align-items: center;
    .title {
      font-size: 14px;
      font-weight: 400;
      color: $text-color-secondary;
    }
    .el-input {
      margin-right: 30px;
    }
    .right {
      margin-left: auto;
      display: flex;
      align-items: center;
      span {
        cursor: pointer;
      }
      .el-icon-delete {
        margin-left: 28px;
      }
    }
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
